﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JGSY.LHBS
{
    public partial class FrmMouth : Form
    {
        public FrmMouth()
        {
            InitializeComponent();
        }
        //本月收入  支出  收支情况
        decimal thisMonthIn = 0.00M, thisMonthEx = 0.00M, thisMonthInEx = 0.00M;
        //总收支列表
        List<RecordInfo> recordList = new List<RecordInfo>();
        private void FrmMouth_Load(object sender, EventArgs e)
        {
            StatisticsData();

        }

        /// <summary>
        /// 按月统计数据
        /// </summary>
        private void StatisticsData()
        {
            listView1.Items.Clear();
            //加载所有收支记录
            LoadAllRecordList();
            //按月统计收支数据并加载列表
            StatisticsMonthList();
            //加载本月收支数据
            LoadStatInfo();
        }

        /// <summary>
        /// 加载所有收支记录
        /// </summary>
        private void LoadAllRecordList()
        {
            //重置
            thisMonthIn = 0.00M;
            thisMonthEx = 0.00M;
            thisMonthInEx = 0.00M;
            recordList = CommonHelper.GetAllRecordList();//获取所有的记录列表
        }


        /// <summary>
        /// 按月统计收支数据并加载列表
        /// </summary>
        private void StatisticsMonthList()
        {
            //月统计列表
            List<MonthStatInfo> monthList = new List<MonthStatInfo>();
            //筛选出日期列表
            List<string> listDate = recordList.Select(r => r.RecordTime.ToString("yyyy-MM-dd")).Distinct().ToList();
            //获取年份列表
            List<int> years = listDate.Select(d => DateTime.Parse(d).Year).Distinct().ToList();
            //本月月份
            int thisMonth = DateTime.Now.Month;
            //按年  按月 统计
            foreach (int year in years)
            {
                //获取该年份所有的月份
                List<int> yearMonths = listDate.Where(d => DateTime.Parse(d).Year == year).Select(d => DateTime.Parse(d).Month).Distinct().ToList();
                //当前年份所有的收支记录
                var yrecordList = recordList.Where(r => r.RecordTime.Year == year);
                foreach (int month in yearMonths)
                {
                    decimal monthinTotal = 0.00M, monthexTotal = 0.00M, monthinexTotal = 0.00M;
                    //当前月份的收支记录
                    var mrecordList = yrecordList.Where(r => r.RecordTime.Month == month);
                    foreach (var record in mrecordList)
                    {
                        if (record.ItemType == "收入")
                            monthinTotal += record.Money;
                        else
                            monthexTotal += record.Money;
                    }
                    monthinexTotal = monthinTotal - monthexTotal;
                    //本月的收入  支出   收支
                    if (month == thisMonth)
                    {
                        thisMonthIn = monthinTotal;
                        thisMonthEx = monthexTotal;
                        thisMonthInEx = monthinexTotal;
                    }
                    MonthStatInfo monthInfo = new MonthStatInfo()
                    {
                        Year = year.ToString(),
                        Month = month.ToString(),
                        InTotal = monthinTotal,
                        ExTotal = monthexTotal,
                        InExTotal = monthinexTotal
                    };
                    monthList.Add(monthInfo);
                }
            }
            //统计列表加载
            if (monthList.Count > 0)
            {
                foreach (MonthStatInfo item in monthList)
                {
                    ListViewItem li = new ListViewItem();
                    li.Text = item.Year;
                    li.SubItems.Add(item.Month);
                    li.SubItems.Add("￥" + item.InTotal.ToString("0.00"));
                    li.SubItems.Add("￥" + item.ExTotal.ToString("0.00"));
                    li.SubItems.Add("￥" + item.InExTotal.ToString("0.00"));
                    listView1.Items.Add(li);
                }
            }

        }
        /// <summary>
        /// 加载本月收支数据
        /// </summary>
        private void LoadStatInfo()
        {
            label2.Text = "￥" + thisMonthIn.ToString("0.00") + " 元";
            label5.Text = "￥" + thisMonthEx.ToString("0.00") + " 元";
            label6.Text = "￥" + thisMonthInEx.ToString("0.00") + " 元";
        }
        private void button3_Click(object sender, EventArgs e)
        {
            StatisticsData();
        } 
    }
}
